package com.jinly.ryder.flow.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinly.ryder.flow.dao.FlowApplyMapper;
import com.jinly.ryder.flow.dao.FlowAuditMapper;
import com.jinly.ryder.flow.dao.entity.FlowApplyDO;
import com.jinly.ryder.flow.dao.entity.FlowAuditDO;
import com.jinly.ryder.flow.service.FlowApplyQueryService;
import com.jinly.ryder.flow.service.mapping.FlowApplyMapping;
import com.jinly.ryder.flow.service.mapping.FlowAuditMapping;
import com.jinly.ryder.flow.service.model.FlowApply;
import com.jinly.ryder.flow.service.model.FlowAudit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class FlowApplyQueryServiceImpl implements FlowApplyQueryService {

    private final FlowApplyMapper flowApplyMapper;
    private final FlowAuditMapper flowAuditMapper;
    private final FlowApplyMapping flowApplyMapping;
    private final FlowAuditMapping flowAuditMapping;

    /**
     * 申请详情
     *
     * @param applyId
     * @return
     */
    @Override
    public FlowApply applyDetail(Long applyId) {
        FlowApply flowApply = flowApplyMapping.toModel(flowApplyMapper.selectById(applyId));
        return flowApply;
    }

    @Override
    public FlowAudit auditDetail(Long auditId) {
        FlowAudit flowAudit = flowAuditMapping.toModel(flowAuditMapper.selectById(auditId));
        return flowAudit;
    }

    @Override
    public List<FlowAudit> auditList(Long applyId) {
        Wrapper<FlowAuditDO> queryWrapper = new QueryWrapper<FlowAuditDO>().lambda()
                .eq(FlowAuditDO::getApplyId, applyId);
        return flowAuditMapping.toModelList(flowAuditMapper.selectList(queryWrapper));
    }

    /**
     * 用户申请列表分页
     *
     * @param flowApply
     * @return
     */
    @Override
    public Page<FlowApply> userApplyPage(Page page, FlowApply flowApply) {
        Wrapper<FlowApplyDO> queryWrapper = new QueryWrapper<FlowApplyDO>().lambda()
                .eq(FlowApplyDO::getApplyUserId, flowApply.getApplyUserId())
                .eq(flowApply.getApplyStatus() != null, FlowApplyDO::getApplyStatus, flowApply.getApplyStatus())
                .eq(flowApply.getAppName() != null, FlowApplyDO::getAppName, flowApply.getAppName())
                .like(flowApply.getApplyTitle() != null, FlowApplyDO::getApplyTitle, flowApply.getApplyTitle())
                .select(FlowApplyDO.class, p -> !"apply_form_data".equals(p.getColumn()))
                .orderByDesc(FlowApplyDO::getId);
        return flowApplyMapping.toModelPage(flowApplyMapper.selectPage(page, queryWrapper));
    }

    @Override
    public Page<FlowAudit> userAuditPage(Page page, FlowAudit auditQuery, FlowApply applyQuery) {
        FlowAuditDO auditQueryDO = flowAuditMapping.toEntity(auditQuery);
        FlowApplyDO flowApplyDO = flowApplyMapping.toEntity(applyQuery);
        page.setOptimizeCountSql(false);
        return flowAuditMapping.toModelPage(flowAuditMapper.selectByPage(page, auditQueryDO, flowApplyDO));
    }

    @Override
    public List<FlowApply> batchQueryApply(Set<Long> applyIds) {
        if (CollectionUtils.isEmpty(applyIds)) return Collections.EMPTY_LIST;
        return flowApplyMapping.toModelList(flowApplyMapper.selectBatchIds(applyIds));
    }
}
